<!-- 
  MIT License

  Copyright (c) 2023 小辉不会飞

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
-->
<!-- animatedGif GIF动画 -->
<template>
  <div class="container">
    <main-header showBack :title="route.query.name"></main-header>
    <div class="main">
      <div id="map" class="map"></div>
    </div>
    <main-footer></main-footer>
  </div>
</template>

<script setup>
// VUE相关
import { onMounted, reactive } from "vue";
import { useRoute } from "vue-router";
// 项目组件
import mainHeader from "@/components/mainHeader.vue";
import mainFooter from "@/components/mainFooter.vue";
// OL相关
import Feature from "ol/Feature";
import Map from "ol/Map";
import Point from "ol/geom/Point";
import View from "ol/View";
import { Icon, Style } from "ol/style";
import { Stamen, Vector as VectorSource } from "ol/source";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import "ol/ol.css";

const route = useRoute(); // 路由对象

const ol = reactive({
  map: null,
  iconFeature: null,
  source: null,
  vectorLayer: null,
});

onMounted(() => {
  getVectorLayer();
  initMap();
  transformGIF();
  pointerWhenMouseover();
});

/**
 * 获取矢量图层
 */
const getVectorLayer = () => {
  ol.iconFeature = new Feature({
    geometry: new Point([0, 0]),
  });
  ol.source = new VectorSource({
    features: [ol.iconFeature],
  });
  ol.vectorLayer = new VectorLayer({
    source: ol.source,
  });
};

/**
 * 初始化地图
 */
const initMap = () => {
  const rasterLayer = new TileLayer({
    source: new Stamen({
      layer: "toner",
    }),
  });
  // 初始化地图到指定DOM元素
  ol.map = new Map({
    layers: [rasterLayer, ol.vectorLayer],
    target: document.getElementById("map"),
    view: new View({
      center: [0, 0],
      zoom: 2,
    }),
  });
};

/**
 * 将iconFeature的元素转换为GIF动画
 */
const transformGIF = () => {
  const gifUrl = "data/globe.gif";
  // index.html中引入gifler.min.js
  const gif = gifler(gifUrl);
  gif.frames(
    document.createElement("canvas"),
    function (ctx, frame) {
      if (!ol.iconFeature.getStyle()) {
        ol.iconFeature.setStyle(
          new Style({
            image: new Icon({
              img: ctx.canvas,
              imgSize: [frame.width, frame.height],
              opacity: 0.8,
            }),
          })
        );
      }
      ctx.clearRect(0, 0, frame.width, frame.height);
      ctx.drawImage(frame.buffer, frame.x, frame.y);
      ol.map.render();
    },
    true
  );
};

/**
 * change mouse cursor when over icon
 */
const pointerWhenMouseover = () => {
  ol.map.on("pointermove", function (e) {
    const pixel = ol.map.getEventPixel(e.originalEvent);
    const hit = ol.map.hasFeatureAtPixel(pixel);
    ol.map.getTarget().style.cursor = hit ? "pointer" : "";
  });
};
</script>

<style scoped lang="scss">
#map {
  width: 100%;
  height: 100%;
}
</style>
